function out (L)

   ## usage: out (L)
   ## 
   ## do downscaling and write simulated local data

   global PAR PRJ isANA ADIR SDIR isCUR

   if !isCUR
      wsim = PAR.sim ;
      init(["SIM = \"" PAR.sim{2} "\";"]) ;
      out(L) ;
      PAR.sim = wsim ; clear wsim ;
      init(["SIM = \"" PAR.sim{1} " " PAR.sim{2} "\";"]) ;
   endif

   ldata = fullfile("data", PRJ, PAR.pdd) ;
   odir = fullfile(SDIR, PAR.pdd) ;

   afile = fullfile(ADIR, [strrep(PAR.ana, "/", "_") ".pc.ob"]) ;
   vfile = fullfile(SDIR, [strrep(PAR.ana, "/", "_") ".pc.ob"]) ;

   if (isnewer(fullfile(odir, "PAR.txt"), PAR.mfile, afile, vfile) && !L), return ; endif

   f = dbstack ; printf("\n\t###   %s   ###\n\n", f(1).name) ;

   ## printf("\n") ;
   ## printf("\t####################################\n") ;
   ## printf("\t########                    ########\n") ;
   ## printf("\tDownscaling \t%s\n\tusing \t%s\n", PAR.sim{1}, [ldata PAR.ana ".mod.ob"]) ;
   ## printf("\t########                    ########\n") ;
   ## printf("\t####################################\n") ;
   ## printf("\n") ;

   printf("out: <-- %s\n", ofile = fullfile(ldata, "pdd.ob")) ;
   load(ofile) ;
   printf("out: <-- %s\n", cfile = fullfile(ldata, "clim.ob")) ;
   load(cfile) ;
   printf("out: <-- %s\n", PAR.mfile) ;
   load(PAR.mfile) ;

   vars = pdd.vars ; VAR = sunique(vars) ;

   w = rescale(pdd, "z") ; pdd.rx = w.x ; # debugging probit
   pdd.x = nan2lim(pdd.x, -9999) ;

   ## do the downscaling
   if strcmp(PAR.mod, "rgr")
      [xds X] = dsc(vfile, clim, R) ;
   elseif strcmp(PAR.mod, "rsc")
      xds = pdd ; xds.x = pdd.rx ;
   else
      [xds X] = dsc(vfile, clim, E) ;
   endif 
   xds.x = nan2lim(xds.x, pdd.udf) ;

   ## output
   for v = VAR

      v = v{:} ;
      J = strcmp(vars, v) ;

      fid = fopen(ofile = fullfile(odir, [v ".csv"]), "wt") ;
      fmt = ["Y,M,D" repmat(",%s", 1, sum(J)) "\n"] ;
      fprintf(fid, fmt, pdd.ids(:,J){:}) ;
      fmt = ["%d,%d,%d" repmat(",%.4g", 1, sum(J)) "\n"] ;
      fprintf(fid, fmt, [xds.id(:,1:3) xds.x(:,J)]') ;
      fclose(fid) ;

      # csvwrite(ofile = [opfx v ".csv"], [xds.id(:,1:3) xds.x(:,J)], "precision", PAR.precision) ;
      printf("out: --> %s\n", ofile) ;

   endfor

   pfile = fullfile(odir, "PAR.txt") ;
   fprintf(fid = fopen(pfile, "wt"), "%s", disp(PAR)) ; fclose(fid) ;
   printf("out: --> %s\n", pfile) ;
   
endfunction
